<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller {
	
	private $currUser;
	
	function __construct() {
        parent::__construct();
		$this->currUser = $this->session->userdata('currUser');
		$this->load->vars(array('currUser' => $this->currUser));
    }
	
	public function index() {
		$data = $this->user_model->getUser_all();
		$this->load->view('user/all', array('users' => $data));
	}

	/**
	 * Controller for Add New User
	 */
	public function add() {
		$this->load->view('user/add');
	}
	
	/**
	 * Controller to get "current user profile" view
	 */
	public function profile() {
		$this->edit();
	}
	
	/**
	 * Controller to get "edit user" view
	 */
	public function edit($uid = 0) {
		$uid = $uid == 0 ? $this->currUser['id'] : $uid;
		$user = $this->user_model->getUser_by_id($uid);
		if($user) {
			$user->current = $this->currUser['id'] == $uid ? 1 : 0;
			$this->load->view('user/edit', array('user' => $user));
		} else
			show_404();
	}
	
	/**
	 * Ajax call to get "add a new user" view
	 */
	public function ajax_add_new_user() {
		if(!check_action_role(__FUNCTION__)) {
			echo json_encode(array('err' => -1, 'msg' => 'Permission Denied'));
			die();
		}
		
		$arr = $this->user_model->addUser_validate($_POST);
		if($arr['err'] == 0) {
			extract($_POST);
			$role = !isset($role) ? 1 : $role;
			$data = array(
				'f_name' => trim($fname),
				'l_name' => trim($lname),
				'user' => trim($username),
				'pass' => $password,
				'email' => trim($email),
				'role' => $role
			);
			$arr['id'] = $this->user_model->addUser($data);
                        $arr['err'] = $arr['id'] ? 0 : -1;
		}
		echo json_encode($arr);
	}
	
	/**
	 * Ajax call to edit user
	 */
	public function ajax_edit_user() {
		if(!check_action_role(__FUNCTION__)) {
			echo json_encode(array('err' => -1, 'msg' => 'Permission Denied'));
			die();
		}
		$arr = $this->user_model->editUser_validate($_POST);
		if($arr['err'] == 0) {
			extract($_POST);
			$role = !isset($role) ? 1 : $role;
			$data = array(
				'id' => $id,
				'user' => trim($username),
				'f_name' => trim($fname),
				'l_name' => trim($lname),
				'email' => trim($email),
				'role' => $role
			);
			if ($newpassword != '')
				$data['pass'] = $newpassword;
			$arr['err'] = $this->user_model->editUser($data) ? 0 : -1;
		}
		echo json_encode($arr);
	}
	
	/**
	 * Ajax call to remove a user out of DB
	 */
	public function ajax_remove_user() {
		if(!check_action_role(__FUNCTION__)) {
			echo json_encode(array('err' => -1, 'msg' => 'Permission Denied'));
			die();
		}
		
		if(!isset($_POST['uid'])) {
			echo json_encode (array('err' => -1, 'msg' => 'Missing parameter.'));
			die;
		} 
		
		$uid = $_POST['uid'];
		$rm = $this->user_model->removeUser($uid);
		if ($rm)
			echo json_encode (array('err' => 0, 'msg' => 'Successfull.'));
		else
			echo json_encode (array('err' => -1, 'msg' => 'Something wrong...'));
	}
	
	/**
	 * Update user status Active:1 or Deactive:0
	 */
	public function ajax_edit_user_status() {
		if(!check_action_role(__FUNCTION__)) {
			echo json_encode(array('err' => -1, 'msg' => 'Permission Denied'));
			die();
		}
		
		if(!isset($_POST['uid']) || !isset($_POST['status'])) {
			echo json_encode (array('err' => -1, 'msg' => 'Missing parameter.'));
			die;
		}
		
		$uid = $_POST['uid'];
		$status = $_POST['status'];
		$result = $this->user_model->editUserStatus($uid, $status);
		if ($result)
			echo json_encode (array('err' => 0, 'msg' => 'Successfull.'));
		else
			echo json_encode (array('err' => -1, 'msg' => 'Something wrong...'));
	}
}
